package ee.ttu.rakarh.commands;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import ee.ttu.rakarh.model.data.DataType;
import ee.ttu.rakarh.model.data.dao.ItemDAO;
import ee.ttu.rakarh.model.data.dao.ItemDAOImpl;
import ee.ttu.rakarh.model.data.dao.ItemTypeDAO;
import ee.ttu.rakarh.model.data.dao.ItemTypeDAOImpl;
import ee.ttu.rakarh.model.data.entities.Item;
import ee.ttu.rakarh.model.data.entities.ItemAttribute;
import ee.ttu.rakarh.model.data.entities.ItemAttributeType;
import ee.ttu.rakarh.model.data.entities.ItemType;
import ee.ttu.rakarh.model.data.entities.TypeAttribute;
import ee.ttu.rakarh.model.form.ItemAttributeForm;
import ee.ttu.rakarh.model.form.ItemForm;
import ee.ttu.rakarh.services.ItemAttributeService;

public class AddItem  implements Command {
	public static final Logger LOG = Logger.getLogger(AddItem.class);
	@Override
	public Boolean execute(HttpServletRequest request,
			HttpServletResponse response) {
		
		String stringTypeId = request.getParameter("type");
		if(stringTypeId == null)
			return false;
		int typeId;
		try{
			typeId = Integer.parseInt(stringTypeId);
		} catch (NumberFormatException e) {
			LOG.error(e);
			return false;
		}
		ItemForm itemForm = new ItemForm();
		
		itemForm.setName(request.getParameter("name"));
		itemForm.setSalePrice(request.getParameter("salePrice"));
		itemForm.setProducer(request.getParameter("producer"));
		itemForm.setDescription(request.getParameter("description"));
		itemForm.setProducerCode(request.getParameter("producerCode"));
		//itemType
		
		
		ItemAttributeService itemAttributeService = new ItemAttributeService();
		Collection<TypeAttribute> typeAttributes = itemAttributeService.getTypeAttributesByTypeId(typeId);
		
		Collection<ItemAttributeForm> itemAttributeForms = new ArrayList<ItemAttributeForm>();
	
		System.out.println("Attributes:");
		for (TypeAttribute typeAttribute : typeAttributes) {
			String itemAttributeValue = request.getParameter(Integer.toString(typeAttribute.getItemAttributeType().getId()));
			Boolean isReq = typeAttribute.isRequired();
			itemAttributeValue = itemAttributeValue == null ? "" : itemAttributeValue;
			ItemAttributeForm itemAttributeForm = new ItemAttributeForm();
			itemAttributeForm.setValue(itemAttributeValue);
			
			itemAttributeForm.setIsReq(isReq);
			
			ItemAttributeType itemAttributeType = new ItemAttributeType();			
			itemAttributeType = typeAttribute.getItemAttributeType();
			
			itemAttributeForm.setItemAttributeType(itemAttributeType);
			
			itemAttributeForms.add(itemAttributeForm);
		}
		
		itemForm.setItemAttributeForms(itemAttributeForms);
		Item item = new Item();
		Boolean res = itemForm.convertToModel(item);
		System.out.println(res);
		if(res == true){
			ItemDAO itemDAO = new ItemDAOImpl();
			ItemTypeDAO itemTypeDAO = new ItemTypeDAOImpl();
			ItemType itemType = itemTypeDAO.getItemTypeById(typeId);
			if(itemType != null){
				item.setItemType(itemType);
				itemDAO.insertItem(item);
			}
		}else{
			System.out.print("nooooooo");
		}
		Set<ItemAttribute> atr = item.getItemAttributes();
		
//		System.out.println("getDescription" + item.getDescription());
//		System.out.println("getName" + item.getName());
//		System.out.println("getProducer()" + item.getProducer());
//		System.out.println("getProducerCode()" + item.getProducerCode());
//		System.out.println("getSalePrice()" + item.getSalePrice());
//		System.out.println("getCreated()" + item.getCreated());
//		
//		for (ItemAttribute itemAttribute : atr) {
//			String str = itemAttribute.getItemAttributeType().getDataType() == DataType.NUMBER ? itemAttribute.getValueNumber() + "" : itemAttribute.getValueText();
//			System.out.println(str + " !!!!!!!!!!!!!!!!");
//		}
		
		if(!res){
			String errorsString = itemForm.getErrors().toString();
			errorsString = errorsString.substring(1, errorsString.length() - 1);
			request.setAttribute("result", errorsString.trim());
			LOG.info("form errors" + errorsString.trim());
		}else{
			request.setAttribute("result", "");
		}
		return true;
	}

}
